import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';

class InstallPlugin {
  static const MethodChannel _channel = const MethodChannel('install_plugin');

  /// for Android : install apk by its file absolute path | for iOS: go to app store by the url;
  /// if the target platform is higher than android 24:
  /// a [appId] is not required
  /// (the caller's applicationId which is defined in build.gradle)
  static Future<dynamic> install(String filePathOrUrlString, String ButtonName,
      {String ohosAppId = ''}) async {
    if (Platform.operatingSystem == 'ohos') {
      if (ButtonName == 'installApk') {
        return installApk(filePathOrUrlString, ohosAppId: ohosAppId);
      } else {
        return goStore(filePathOrUrlString);
      }
    }
    return {'isSuccess': false, 'errorMessage': 'unsupported device'};
  }

  /// for Android : install apk by its file absolute path;
  /// if the target platform is higher than android 24:
  /// a [appId] is not required
  /// (the caller's applicationId which is defined in build.gradle)
  static Future<dynamic> installApk(String filePath,
      {String ohosAppId = ''}) async {
    Map<String, String> params = {'filePath': filePath,'packageName':ohosAppId};
    final dynamic result = await _channel.invokeMethod('installApk', params);
    return result;
  }

  static Future<dynamic> goStore(String filePath) async {
    Map<String, String> params = {'filePath': filePath};
    final dynamic result = await _channel.invokeMethod('goStore', params);
    return result;
  }
}